<template>
  <div class="hot-container">
    <div class="hot-chart" ref="chartRef"></div>
    <span class="iconfont arr-left" :style="fontSizeStyle" @click="handleLeft">&#xe6ef;</span>
    <span class="iconfont arr-right" :style="fontSizeStyle" @click="handleRight">&#xe6ed;</span>
    <span class="title" :style="fontSizeStyle">{{ titleName }}</span>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { getThemeValue } from '../utils/theme_utils.js'

export default {
  name: 'Hot',
  data() {
    return {
      chartInstance: null,
      allData: null,
      currentIndex: 0, // 当前一级分类的索引
      titleFontSize: 0
    }
  },
  computed: {
    titleName() {
      if(!this.allData) {
        return ''
      } else {
        return this.allData[this.currentIndex].name
      }
    },
    fontSizeStyle() {
      return {
        fontSize: this.titleFontSize + 'px',
        color: getThemeValue(this.theme).titleColor
      }
    },
    ...mapState(['theme'])
  },
  watch: {
    theme() {
      // 主题改变时，销毁当前图表
      this.chartInstance.dispose()
      // 以最新的主题初始化图表
      this.initChart()
      this.screenAdapter()
      this.updateChart()
    }
  },
  created() {
    this.$socket.registerCallBack('hotData', this.getData)
  },
  mounted() {
    this.initChart()
    //this.getData()
    this.$socket.send({
      action: 'getData',
      socketType: 'hotData',
      chartName: 'hotproduct',
      value: ''
     })
    window.addEventListener('resize',this.screenAdapter)
    this.screenAdapter()
  },
  destroyed() {
    window.removeEventListener('resize',this.screenAdapter)
    this.$socket.unRegisterCallBack('hotData')
  },
  methods: {
    initChart() {
      this.chartInstance = this.$echarts.init(this.$refs.chartRef, this.theme)
      const initOption = {
        title: {
          text: '┃ 热销商品的占比',
          left: 20,
          top: 20
        },
        legend: {
          top: '15%',
          icon: 'circle'
        },
        tooltip: {
          show: true,
          formatter: arg => {
            const thirdCategory = arg.data.children
            let total = 0
            thirdCategory.forEach(item => {
              total += item.value
            })
            let result = ''
            thirdCategory.forEach(item => {
              result += `
                ${item.name}: ${(item.value / total * 100).toFixed(2) + '%'}
                </br>
              `
            })
            return result
          }
        },
        series: [
          {
            type: 'pie',
            label: {
              show: false
            },
            // 饼图高亮时的样式
            emphasis: {
              label: {
                show: true
              },
              labelLine: {
                show: false
              }
            }
          }
        ],
      }
      this.chartInstance.setOption(initOption)
    },
    
    getData(result) {
      //const result = await this.$http.get('hotproduct')
      this.allData = result
      this.updateChart()
    },

    updateChart() {
      const legendData = this.allData[this.currentIndex].children.map(item => {
        return item.name
      })
      const seriesData = this.allData[this.currentIndex].children.map(item => {
        return {
          name: item.name,
          value: item.value,
          children: item.children
        }
      })
      const dataOption = {
        legend: {
          data: legendData
        },
        series: [
          {
            data: seriesData
          }
        ]
      }
      this.chartInstance.setOption(dataOption)
    },

    // 屏幕适配
    screenAdapter() {
      this.titleFontSize = this.$refs.chartRef.offsetWidth / 100 * 3.6
      const adapterOption = {
        title: {
          textStyle: {
            fontSize: this.titleFontSize
          }
        },
        series: [
          {
            radius: this.titleFontSize * 4.5,
            center: ['50%','60%']
          }
        ],
        legend: {
          itemWidth: this.titleFontSize,
          itemHeight: this.titleFontSize,
          itemGap: this.titleFontSize / 2,
          textStyle: {
            fontSize: this.titleFontSize / 2,
          }
        },
      }
      this.chartInstance.setOption(adapterOption)
      this.chartInstance.resize()
    },

    // 左箭头
    handleLeft() {
      this.currentIndex--
      if(this.currentIndex < 0) {
        this.currentIndex = this.allData.length - 1
      }
      this.updateChart()
    },

    // 右箭头
    handleRight() {
      this.currentIndex++
      if(this.currentIndex > this.allData.length - 1) {
        this.currentIndex = 0
      }
      this.updateChart()
    },
  },
}
</script>

<style scoped>
.title {
  position: absolute;
  left: 80%;
  bottom: 20px;
  color: #fff;
}
.arr-left {
  position: absolute;
  top: 50%;
  left: 10%;
  transform: translateY(-50%);
  cursor: pointer;
  color: #fff;
}
.arr-right {
  position: absolute;
  top: 50%;
  right: 10%;
  transform: translateY(-50%);
  cursor: pointer;
  color: #fff;
}
</style>